package com.yunkazaixian.syntelecom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunkazaixian.syntelecom.entity.*;
import com.yunkazaixian.syntelecom.mapper.McProductNormsMapper;
import com.yunkazaixian.syntelecom.mapper.McSimDxMapper;
import com.yunkazaixian.syntelecom.mapper.McSimFlowMapper;
import com.yunkazaixian.syntelecom.service.DXService;
import com.yunkazaixian.syntelecom.service.IMcSimFlowService;
import com.yunkazaixian.syntelecom.utils.DateUtils;
import com.yunkazaixian.syntelecom.utils.ListUtils;
import com.yunkazaixian.syntelecom.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 手机卡 前端控制器
 * </p>
 *
 * @author Lin
 * @since 2024-03-21
 */
@Controller
@RequestMapping("/test")
public class TestController {


    @Autowired
    DXService dxService;

    @Autowired
    McProductNormsMapper normsMapper;

    @Autowired
    IMcSimFlowService simFlowService;

    @Autowired
    McSimDxMapper simDxMapper;

    @Autowired
    McSimFlowMapper flowMapper;


    @Autowired
    McProductNormsMapper mcProductNormsMapper;

    /**
     * 测试
     *
     * @return
     */
    @RequestMapping("/test")
    @ResponseBody
    @Transactional
    public Object test(HttpServletResponse response) {


            McSimDx sim = simDxMapper.selectOne(new LambdaQueryWrapper<McSimDx>().eq(McSimDx::getIccid,"8986112320003964919"));
            if (sim == null) {
                return R.fail("查询卡异常");
            }


            return R.ok(sim);


    }


    private HttpResponse setArriveTime(McSimDx sim) {


        if (StringUtils.isEmpty(sim.getBatchNo())) {
            return R.fail("未分卡");
        }

        QueryWrapper qw = new QueryWrapper();
        qw.eq("norm.del_flag", 0);
        qw.eq("norm.sku", sim.getSku());
        List<McProductNorms> normList = mcProductNormsMapper.selectProduct(qw);
        if (!ListUtils.isEmpty(normList)) {
            McProductNorms norm = normList.get(0);
            if (norm != null) {
                sim.setArriveTime(DateUtils.dateToStr(DateUtils.addMonth(DateUtils.strToDate(sim.getActiveTime()), Integer.parseInt(norm.getDeadline()))));
                System.out.println(sim.getIccid() + " 设置了到期时间=====>" + sim.getArriveTime());
            }
        }

        return R.ok();
    }

    @RequestMapping(value = "/monthFlow")
    @ResponseBody
    public void monthFlow(String iccid, String month) {

        LambdaQueryWrapper<McSimDx> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNotNull(McSimDx::getBatchNo)
                .ne(McSimDx::getBatchNo, "")
                .and(it -> it.isNull(McSimDx::getImZhuPackage)
                        .or()
                        .notLike(McSimDx::getImZhuPackage, "NB"));
        wrapper.eq(McSimDx::getIccid, iccid);


        List<McSimDx> mcSimDxList = simDxMapper.selectList(wrapper);
        Date date = new Date();
        for (McSimDx mcSimDx : mcSimDxList) {
            System.out.println(mcSimDx);
            McSimFlow mcSimFlow = new McSimFlow();
            mcSimFlow.setIccid(mcSimDx.getIccid());
            double flow = simFlowService.monthFlow(mcSimDx.getIccid(), month);
            mcSimFlow.setUsed(Double.toString(flow));
            mcSimFlow.setStartDate(DateUtils.getFirstDayOfMonth(month));
            mcSimFlow.setEndDate(DateUtils.getLastDayOfMonth(month));
            mcSimFlow.setCreateTime(date);
            mcSimFlow.setUpdateTime(date);
            flowMapper.insert(mcSimFlow);
        }


    }
}
